$(document).ready(function() {
    $('#list_exam_questions').find('li').hide().nextAll().hide();

    $('#addQuestion').click( function(){
        var nextQId = getNextQId();
        $(
        '<ul id="q' + nextQId + '_wrapper"' +
            '<li class="question" id="q' + nextQId + '">' +
                '<label>Spurning ' + nextQId + '</label><br />' +
                '<input id="question' + nextQId + '" type="text" name="question' + nextQId + '"  placeholder="Required" required />' +
                '<input id="q' + nextQId + 'Hidden" type="hidden" value="' + nextQId + '" />' +
            '</li>' +
            '<li>' +
                '<ul id="list_q' + nextQId + '_answers">' +

                    '<li id="q' + nextQId + '_a1">' +
                        '<label>Svar 1</label><br />' +
                        '<input type="text" name="q' + nextQId + '_answer1" placeholder="Required" required />' +
                        '<input type="radio" name="rightAnswer_q' + nextQId + '" value="1" checked />' +
                        '<input id="a1Hidden" type="hidden" value="1" />' +
                    '</li>' +

                    '<li id="q' + nextQId + '_a2">' +
                        '<label>Svar 2</label><br />' +
                        '<input type="text" name="q' + nextQId + '_answer2" placeholder="Required" required />' +
                        '<input type="radio" name="rightAnswer_q' + nextQId + '" value="2" />' +
                        '<input id="a1Hidden" type="hidden" value="2" />' +
                    '</li>' +
                '</ul>' +
                '<input type="button" value="+ svar" onclick="addAnswer(this)" />' +
            '</li>' +
        '</ul>'
        ).appendTo('#list_exam_questions').hide().fadeIn(1000);

        $('#question' + nextQId + '').focus();

        $('#deleteQuestion').removeAttr("disabled");

    });

    $('#deleteQuestion').click( function() {
        var qId = getNextQId()-1;
        $('#q' + qId + '_wrapper').fadeOut(1000).delay(1000).remove();
        if(qId==1)
        {
            $('#deleteQuestion').attr("disabled","disabled");
        }
    });

});

function changeQuestionStatus(qId){
    $('#question' + qId + '').css({"color":"green","font-weight":"bold"});
}

function getNextQId(){
    var result = $('.question').last().find('input[type=hidden]').val();
    return parseInt(result)+1;
}

function getThisQId(where){
    var result = $(where).parent().parent().find('input[type=hidden]').val();
    return result;
}

function getNextAId(where){
    var result = $(where).prev().find('li').last().find('input[type=hidden]').val();
    return parseInt(result)+1;
}

function addAnswer(where){
    var thisQId = getThisQId(where);
    var nextAId = getNextAId(where);

    $(
        '<li id="q' + thisQId + '_a' + nextAId + '">' +
            '<label>Svar ' + nextAId + ' </label><br />' +
            '<input type="text" name="q' + thisQId + '_answer' + nextAId + '" placeholder="Required" required />' +
            '<input type="radio" name="rightAnswer_q' + thisQId + '" value="' + nextAId + '" />' +
            '<input id="a' + nextAId + 'Hidden" type="hidden" value="' + nextAId + '"' +
        '</li>'
    ).appendTo('#list_q' + thisQId + '_answers').hide().fadeIn(1000);

    $('#q' + thisQId + '_a' + nextAId + '').find('input[type=text]').focus();
}